package com.wzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzy.entity.Blog;
import com.wzy.mapper.BlogMapper;
import com.wzy.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.util.Const;
import com.wzy.util.Pager;
import com.wzy.util.textTranfromHtmlUtils;
import lombok.With;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2021-02-09
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Resource
    private BlogMapper blogMapper;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    /**
     * 分页查询博客信息
     *
     * @param blogPage 分页
     * @param date     时间
     * @return
     */
    @Override
    public Page<Blog> getBlog(Page<Blog> blogPage, String date) {
        return blogMapper.selectBlogByDate(blogPage, date);
    }

    /**
     * 按年份查询对应的信息
     *
     * @param year 年份
     * @return
     */
    @Override
    public List<Blog> getMonthListByYear(String year) {
        return blogMapper.getMonthListByYear(year);
    }

    /**
     * 查询战役所对应的事件
     *
     * @param battlePage 分页
     * @param battleId   战役id
     * @return
     */
    @Deprecated
    @Override
    public void getbattleDataByBattleId(Page<Blog> battlePage, Long battleId) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.eq("battle_id", battleId);
        wrapper.select("blog_id", "user_id", "province_id", "battle_id", "title",
                "description", "img", "comment_count", "create_time");
        baseMapper.selectPage(battlePage, wrapper);


    }

    /**
     * 根据Id查询Blog
     *
     * @param blogId
     * @return
     */
    @Override
    public Blog getBlogById(Long blogId) {
        Blog blog = null;
        //去缓存中查
        blog = (Blog) redisTemplate.opsForValue().get("blog" + String.valueOf(blogId));
        //缓存中无去数据库中查
        if (blog == null) {
            blog = baseMapper.selectById(blogId);
            String content=textTranfromHtmlUtils.markdownToHtmlExtensions(blog.getContent());
            blog.setContent(content);
            //设置缓存
            redisTemplate.opsForValue().set("blog" + String.valueOf(blogId), blog);
            redisTemplate.expire(String.valueOf(blogId), 5, TimeUnit.MINUTES);
        }
        return blog;
    }

    @Override
    public Pager<Blog> list(Blog condition, Integer page) {
        List<Blog> datas=this.blogMapper.selectByCondition(condition, (page-1)* Const.PAGE_SIZE, Const.PAGE_SIZE);
        int size=this.blogMapper.sizeByCondition(condition);
        return new Pager<>(datas, size, page, Const.PAGE_SIZE);
    }

    @Override
    public int append(Blog blog) {
        //得到所有以blog开头的key
        Set<String> keys = redisTemplate.keys("*blog*");
        redisTemplate.delete(keys);
        blog.setViews(0);
        blog.setCommentCount((long)0);
        blog.setUserId((long)1);
        blog.setCreateTime(new Date());
        return this.baseMapper.insert(blog);
    }

    @Override
    public int modify(Blog blog) {
        Set<String> keys = redisTemplate.keys("*blog*");
        redisTemplate.delete(keys);
        blog.setUpdateTime(new Date());
        return this.baseMapper.updateById(blog);
    }

    @Override
    public Blog detail(Long blogId) {
        return this.blogMapper.selectById(blogId);
    }


}
